
__asciichart = {64:0, 36:2, 223:30, 95:64, 196:91, 214:92, 220:94, 168:96, 228:123, 246:124, 252:162}       
def charToAscii(char):
    o = ord(char)
    try:
        b = __asciichart[o]
    except:
        b = o
    return b

def intlen(b):
    return len(bin(b))-2

def bintoHex(i):
    if i == '':
        return ''
    h = hex(int(i, base=2))
    return h[2:].upper()

def asciiToPDU(string):
    byt = map(charToAscii, string)
    print byt
    print map(bin, byt)
    res = list()
    i = 0
   
    while i < len(byt):
        b = byt[i]
        i += 1

        if intlen(b) == 8:
            res.append(b)
            continue

        print bin(b),':',
        while intlen(b) < 8 and i < len(byt):
            if intlen(byt[i]) < 8 - intlen(b):
                b = (byt[i] << intlen(b)) + b
                i += 1
            else:
                rest = 8 - intlen(b)
                new = byt[i] >> rest
                print bin(byt[i]),'-->',bin(new),
                b = ((b - (new << 1)) << (8-rest))+  b
                byt[i] = new
                break
        print '.',bin(b)
        res.append(b)

    print
    return ' '.join(res), ' '.join(map(bintoHex, res))
    #return ' '.join(map(bintoHex, res))

print asciiToPDU('hellohello') #'E8 32 9B FD 46 97 D9 EC 37'
#print asciiToPDU('Hi!') #C8 74 08 #11001000 01110100 00001000
